package com.pbd.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.dom.client.OptionElement;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;

public class PbdListBox extends Composite {
	private static final String BIDI_ATTR_NAME = "bidiwrapped";
	private ListBox listBox;
	private List keyValueList;

	
	public PbdListBox(String fieldWidth) {
		listBox = new ListBox();
		listBox.setWidth(fieldWidth);
		listBox.setStyleName("dropdown_style");
		initWidget(listBox);
	}

	
	public PbdListBox(String label, String fieldWidth) {
		HorizontalPanel hp = new HorizontalPanel();
		HTML labelHTML = new HTML(label);
		hp.add(labelHTML);
		listBox = new ListBox();
		listBox.setWidth(fieldWidth);
		listBox.setStyleName("dropdown_style");
		hp.add(listBox);
		initWidget(hp);
	}

	public PbdListBox(String label, String fieldWidth, String objectName, String[] columnExpression, String whereClause) {

		HorizontalPanel hp = new HorizontalPanel();
		HTML labelHTML = new HTML(label);
		hp.add(labelHTML);
		listBox = new ListBox();
		listBox.setWidth(fieldWidth);
		hp.add(listBox);
		setValue(objectName, columnExpression, whereClause, null, null, null);
		initWidget(hp);
	}

	/*public BNIListBox(String label, String fieldWidth) {

		HorizontalPanel hp = new HorizontalPanel();
		HTML labelHTML = new HTML(label);
		hp.add(labelHTML);
		listBox = new ListBox();
		listBox.setWidth(fieldWidth);
		listBox.setStyleName("body_txt_black");
		hp.add(listBox);
		initWidget(hp);
	}*/

	public void addValue(String strValue) {
		if (keyValueList == null)
			keyValueList = new ArrayList();
		if (strValue.equalsIgnoreCase("Month")) {
			listBox.addItem("");
			keyValueList.add(getKeyValuePair(-1L, ""));
			listBox.addItem("Jan");
			keyValueList.add(getKeyValuePair(0L, "Jan"));
			listBox.addItem("Feb");
			keyValueList.add(getKeyValuePair(1L, "Feb"));
			listBox.addItem("Mar");
			keyValueList.add(getKeyValuePair(2L, "Mar"));
			listBox.addItem("Apr");
			keyValueList.add(getKeyValuePair(3L, "Apr"));
			listBox.addItem("May");
			keyValueList.add(getKeyValuePair(4L, "May"));
			listBox.addItem("Jun");
			keyValueList.add(getKeyValuePair(5L, "Jun"));
			listBox.addItem("Jul");
			keyValueList.add(getKeyValuePair(6L, "Jul"));
			listBox.addItem("Aug");
			keyValueList.add(getKeyValuePair(7L, "Aug"));
			listBox.addItem("Sep");
			keyValueList.add(getKeyValuePair(8L, "Sep"));
			listBox.addItem("Oct");
			keyValueList.add(getKeyValuePair(9L, "Oct"));
			listBox.addItem("Nov");
			keyValueList.add(getKeyValuePair(10L, "Nov"));
			listBox.addItem("Dec");
			keyValueList.add(getKeyValuePair(11L, "Dec"));
			// return listBox;
		}
		if (strValue.equalsIgnoreCase("Designation")) {

			listBox.addItem("");
			keyValueList.add(getKeyValuePair(-1L, ""));
			listBox.addItem("Director");
			keyValueList.add(getKeyValuePair(0L, "Director"));
			listBox.addItem("Co-ordinator");
			keyValueList.add(getKeyValuePair(1L, "Co-ordinator"));
			listBox.addItem("Secretary");
			keyValueList.add(getKeyValuePair(2L, "Secretary"));
			// return listBox;
		}
		if (strValue.equalsIgnoreCase("Title")) {

			listBox.addItem("");
			keyValueList.add(getKeyValuePair(-1L, ""));
			listBox.addItem("Mr");
			keyValueList.add(getKeyValuePair(0L, "Mr"));
			listBox.addItem("Miss");
			keyValueList.add(getKeyValuePair(1L, "Miss"));
			listBox.addItem("Mrs");
			keyValueList.add(getKeyValuePair(2L, "Mrs"));

		}
	}

	private KeyValuePair getKeyValuePair(Long key, String value) {
		KeyValuePair keyValuePair = new KeyValuePair();
		keyValuePair.setKey(key);
		keyValuePair.setValue(value);
		return keyValuePair;
	}

	public ListBox addDateValue() {
		if (keyValueList == null)
			keyValueList = new ArrayList();
		for (int dateIndex = 1; dateIndex <= 31; dateIndex++) {
			keyValueList.add(getKeyValuePair(Long.valueOf(dateIndex), String.valueOf(dateIndex)));
			listBox.addItem(String.valueOf(dateIndex));
		}
		return listBox;
	}

	public void setValueUsingNative(String query, final String defaultValue) {/*
		String strQuery = null;
		if (query != null) {
			strQuery = "select  weekId, weekNo from weekmaster WHERE convert(substring(weekno,5),unsigned integer) <= 17 and year = '2012'";

			ServiceUtil.getHibernateServiceAsync().getSearchResult(strQuery, new AsyncCallback() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(Object result) {
					listBox.clear();
					List dataList = (List) result;
					System.err.println("DATA LIST VALUES :: "+dataList.size() + dataList.toString());
					keyValueList = new ArrayList();
					KeyValuePair defaultkeyValuePair = new KeyValuePair();
					defaultkeyValuePair.setKey(-1L);
					defaultkeyValuePair.setValue("---Select---");
					keyValueList.add(defaultkeyValuePair);
					listBox.addItem("---Select---");
					if (dataList.size() > 0) {
						for (int index = 0; index < dataList.size(); index++) {
							ArrayList valueList = (ArrayList) dataList.get(index);
							if (valueList.size() > 0) {
								KeyValuePair keyValue = new KeyValuePair();
								keyValue.setKey(Long.valueOf((Integer) valueList.get(0)));
								keyValue.setValue((String) valueList.get(1));
								keyValueList.add(keyValue);
								listBox.addItem((String) valueList.get(1));
							}
							// setValueList(keyValueList);
						}
						if (defaultValue != null)
							setValue(defaultValue);
						else
							setValue("---Select---");
					}

				}

			});
		}
	*/}

	public void setValue(String objectName, String[] columnExpression, String whereClause, String orderBy, String query, final String defaultValue) {/*

		ServiceUtil.getHibernateServiceAsync().findObjects(objectName, columnExpression, whereClause, orderBy, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(Object result) {
				listBox.clear();
				List dataList = (List) result;
				keyValueList = new ArrayList();
				KeyValuePair defaultkeyValuePair = new KeyValuePair();
				defaultkeyValuePair.setKey(-1L);
				defaultkeyValuePair.setValue("---Select---");
				keyValueList.add(defaultkeyValuePair);
				listBox.addItem("---Select---");
				if (dataList.size() > 0) {
					for (int index = 0; index < dataList.size(); index++) {
						ArrayList valueList = (ArrayList) dataList.get(index);
						if (valueList.size() > 0) {
							KeyValuePair keyValue = new KeyValuePair();
							keyValue.setKey((Long) valueList.get(0));
							keyValue.setValue((String) valueList.get(1));
							keyValueList.add(keyValue);
							listBox.addItem((String) valueList.get(1));
						}
						// setValueList(keyValueList);
					}
					if (defaultValue != null)
						setValue(defaultValue);
					else
						setValue("---Select---");
				}
			}

		});
	*/}

	public void setValueList(List<KeyValuePair> keyValueList) {
		this.keyValueList = keyValueList;
		int size = keyValueList == null ? 0 : keyValueList.size();
		for (int index = 0; index < size; index++) {
			KeyValuePair keyValuePair = (KeyValuePair) keyValueList.get(index);
			listBox.addItem(keyValuePair.getValue());
			//System.err.println("KEY VALUE :: " + keyValuePair.getValue());
		}

	}

	public Long getKey(String value) {
		if (keyValueList == null || keyValueList.size() == 0)
			return -1L;
		Iterator keyValueIt = keyValueList.iterator();
		while (keyValueIt.hasNext()) {
			KeyValuePair keyValuePair = (KeyValuePair) keyValueIt.next();
			if (keyValuePair.getValue().equalsIgnoreCase(value)) {
				return keyValuePair.getKey();
			}
		}
		return -1L;
	}

	public String getValue(Long key) {
		if (keyValueList == null || keyValueList.size() == 0)
			return "";
		Iterator keyValueIt = keyValueList.iterator();
		while (keyValueIt.hasNext()) {
			KeyValuePair keyValuePair = (KeyValuePair) keyValueIt.next();
			if (keyValuePair.getKey().equals(key)) {
				return keyValuePair.getValue();
			}
		}
		return "";
	}

	public String getValue() {
		return listBox.getItemText(listBox.getSelectedIndex());
	}

	public int getSelectedIndex() {
		return getSelectElement().getSelectedIndex();
	}

	private SelectElement getSelectElement() {
		return getElement().cast();
	}

	public String getItemText(int index) {
		checkIndex(index);
		return getOptionText(getSelectElement().getOptions().getItem(index));
	}

	protected String getOptionText(OptionElement option) {
		String text = option.getText();
		if (option.hasAttribute(BIDI_ATTR_NAME) && text.length() > 1) {
			text = text.substring(1, text.length() - 1);
		}
		return text;
	}

	private void checkIndex(int index) {
		if (index < 0 || index >= getItemCount()) {
			throw new IndexOutOfBoundsException();
		}
	}

	public int getItemCount() {
		return getSelectElement().getOptions().getLength();
	}

	public void setValue(String value) {
		int size = keyValueList == null ? 0 : keyValueList.size();
		System.err.println("keyValueList:"+size);
		for (int index = 0; index < size; index++) {
			KeyValuePair keyValuePair = (KeyValuePair) keyValueList.get(index);
			if (keyValuePair.getValue().equalsIgnoreCase(value)) {
				listBox.setItemSelected(index, true);
				break;
			}
		}

	}

	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
		return listBox.addChangeHandler(handler);
	}

	public void addChangeListener(ChangeListener listener) {
		listBox.addChangeListener(listener);
	}

}
